﻿using Sidvall.Serialization;

namespace Sidvall.Data
{
    public class DataRowContext
    {
        #region Public Members

        #region DataRowKey

        private string _DataRowKey;
        public string DataRowKey
        {
            get
            {
                return _DataRowKey;
            }
            set
            {
                _DataRowKey = value;
            }
        }

        #endregion
        #region DataRowState

        private DataRowState _DataRowState;
        public DataRowState DataRowState
        {
            get
            {
                return _DataRowState;
            }
            set
            {
                _DataRowState = value;
            }
        }

        #endregion

        #region Copy

        public DataRowContext Copy()
        {
            DataRowContext item;

            item = new DataRowContext()
            {
                _DataRowState = this._DataRowState,
                _DataRowKey = this._DataRowKey
            };

            return item;
        }

        public DataRowContext Copy(DataRowState? dataRowState)
        {
            DataRowContext item;

            item = new DataRowContext()
            {
                _DataRowState = dataRowState ?? this._DataRowState,
                _DataRowKey = this._DataRowKey
            };

            return item;
        }

        #endregion
        #region Import

        public void Import(IDataRowContextProvider item, DataRowState? dataRowState)
        {
            if (dataRowState != null)
                this._DataRowState = dataRowState.Value;
            else if (item != null)
                this._DataRowState = DataRowContextManager.GetDataRowState(item);
        }
        public void Import(IDataRowContextProvider item, DataRowState? dataRowState, string dataRowKey)
        {
            Import(item, dataRowState);
            this._DataRowKey = dataRowKey;
        }

        #endregion
        #region SetDataRowState

        public bool SetDataRowState(DataRowState dataRowState, bool byForce)
        {
            if (byForce)
            {
                this._DataRowState = dataRowState;
                return true;
            }
            else
            {
                switch (dataRowState)
                {
                    case DataRowState.Added:
                    case DataRowState.Modified:
                        switch (this._DataRowState)
                        {
                            case DataRowState.Unchanged:
                                this._DataRowState = dataRowState;
                                return true;
                            default:
                                return false;
                        }
                    case DataRowState.Deleted:
                        switch (this._DataRowState)
                        {
                            case DataRowState.Deleted:
                                return false;
                            default:
                                this._DataRowState = dataRowState;
                                return true;
                        }
                    default:
                        return false;
                }
            }
        }

        #endregion
        #region ToString

        public override string ToString()
        {
            return this.DataRowState.ToString().Append(this.DataRowKey, ": ");
        }

        #endregion

        #endregion
        #region Constructors

        public DataRowContext()
            : this(DataRowState.Added, null)
        {
        }

        public DataRowContext(DataRowState dataRowState)
            : this(dataRowState, null)
        {
        }

        public DataRowContext(DataRowState dataRowState, string dataRowKey)
        {
            this._DataRowState = dataRowState;
            this._DataRowKey = dataRowKey;
        }

        #endregion
    }
}
